var product_detail = new Vue({
    el: "#detail",
    data: {
        product: [],
        reviews:[],
        reviewList:[],
        flag:0
    },
    mounted: function () {
        this.getDetail(1);
    },
    methods: {
        getDetail: function (pageNum) {
            var id = getQueryVariable("id");
            axios.get("/product/id/" + id,{
                params:{"pageNum":pageNum},
            }).then((response)=>{
                if (response.data.code == 200) {
                    product_detail.product = response.data.data;
                    product_detail.reviewList =response.data.data.reviews.list;
                    product_detail.reviews=response.data.data.reviews;
                } else {
                    window.location.href = "/view/other/404.html";
                }
            }).then(function () {
                $('.product-large-slider').not('.slick-initialized').slick({
                    fade: true,
                    arrows: false,
                    asNavFor: '.pro-nav'
                });
                // product details slider nav active
                $('.pro-nav').not('.slick-initialized').slick({
                    slidesToShow: 4,
                    asNavFor: '.product-large-slider',
                    centerMode: true,
                    arrows: false,
                    rtl: true,
                    centerPadding: 0,
                    focusOnSelect: true,
                    responsive: [{
                        breakpoint: 576,
                        settings: {
                            slidesToShow: 3,
                        }
                    }]
                });
              if (product_detail.flag==0){
                  $('.pro-qty').prepend('<span class="dec qtybtn">-</span>');
                  $('.pro-qty').append('<span class="inc qtybtn">+</span>');
                  $('.qtybtn').on('click', function () {
                      var $button = $(this);
                      var oldValue = $button.parent().find('input').val();
                      if ($button.hasClass('inc')) {
                          var newVal = parseFloat(oldValue) + 1;
                      } else {
                          // Don't allow decrementing below zero
                          if (oldValue > 1) {
                              var newVal = parseFloat(oldValue) - 1;
                          } else {
                              newVal = 1;
                          }
                      }
                      $button.parent().find('input').val(newVal);
                  });
                  product_detail.flag=1;
              }
            });
        },
        addCart: function (id,num1) {
            var num=parseInt($("#pnum").val());
            var cart = handleLocalStorage("get", "cart");
            var flag = false;
            if (!cart) {
                var item = id + "-" + num + ",";
                handleLocalStorage("set", "cart", item);
            } else {
                //cart=cart.substring(0,cart.length-1);
                var arr = cart.split(',');
                var arrItem;
                for (var i = 0; i < arr.length - 1; i++) {
                    arrItem = arr[i].split("-");
                    if (arrItem[0] == id) {
                        arrItem[1] = parseInt(arrItem[1]) + num;
                        arr[i] = arrItem[0] + "-" + arrItem[1];
                        handleLocalStorage("set", "cart", arr);
                        flag = true;
                    }
                }
                if (!flag) {
                    cart = cart + id + "-" + num + ",";
                    handleLocalStorage("set", "cart", cart);
                }
            }
            cart_view.getCart();
            alert("添加购物车成功！")
        }
    }
});

function subReview() {
    var reviewContent = $("#content").val();
    var star = $('input:radio:checked').val();
    $.ajax({
        url: "/user/review",
        type: "POST",
        headers: {
            "Authorization": "Bearer " + handleLocalStorage("get", "token")
        },
        contentType: "application/json;charset=utf-8",
        dataType: 'json',
        data: JSON.stringify({
            "pid":product_detail.product.id,
            "content": reviewContent,
            "star": parseInt(star)
        }),
        success: function (response) {
            if (response.code == 200) {
                $("#content").val('');
                product_detail.getDetail(1);
            } else {
                alert(response.message);
            }
        },
        error: function (error) {
            alert("服务器错误");
        }
    });
}
